﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;

namespace PhoneClassLibraryJSON
{
    public class Result : IResult
    {
      //  JObject job;
        string id;
        string nom; 
        string prix;
        string store;
        string image;

        public static List<IResult> json2Result( JObject json ) {
            List<IResult> res = new List<IResult>();

            JArray jarray = json["result"] as JArray;

            for (int i = 0; i < jarray.Count; i++) {
                if (jarray[i].First == null) {
                    continue;
                }
                String id = "" + jarray[i]["id"];
                String nom = jarray[i]["name"].ToString();
                String prix = jarray[i]["priceMin"].ToString() + " à " + jarray[i]["priceMax"].ToString();
                String store = jarray[i]["store"].ToString();
                String image;
                try {
                    image = jarray[i]["image"].ToString();
                } catch (Exception) {
                    image = "";
                }

                res.Add( new Result( id, nom, prix, store, image ) );
            }


            return res;
        }


        
        private Result( string id, string nom , string prix, string store, string image){
            this.id = id;
            this.nom = nom;
            this.prix = prix;
            this.store = store;
            this.image = image;
        }

        public string Id {
            get {
                return id;
            }
        }

        public string Nom {
            get {
                return nom;
            }
        }

        public string Prix {
            get {
                return prix;
            }
        }

        public string Store {
            get {
                return store;
            }
        }

        public string Image {
            get {
                return image;
            }
        }
    }
}
